package edu.usc.tweetool.servlet;

import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;

import edu.usc.tweetool.logic.LoginProcess;
import edu.usc.tweetool.logic.UserProcess;

public class DemoServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public DemoServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		WebApplicationContext applicationContext = WebApplicationContextUtils
				.getRequiredWebApplicationContext(getServletContext());

		UserProcess up = (UserProcess) applicationContext
				.getBean("UserProcess");

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		String pin = request.getParameter("pin");

		boolean login = false;

		if (pin == null || pin.length() == 0) {
			login = LoginProcess.getInstance().login(null);
		} else {
			login = LoginProcess.getInstance().login(pin);
		}

		if (!login) {
			System.out.println(LoginProcess.REDIRECT_URL);
			response.sendRedirect(LoginProcess.REDIRECT_URL);
			return;
		}

		up.setTwitter(LoginProcess.getInstance().getTwitter());
		up.processLogin();

		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>Tweetool Test Platform</TITLE></HEAD>");
		out.println("  <BODY>");
		out.println("<form action='servlet/UpdateServlet'>");
		out.println("update your status: ");
		out.println("<input type='text' name='status'/>");
		out.println("<input type='submit' name='click' value='click'/>");
		out.println("</form>");
		out.println("Waiting......Click and get Report");
		out.println(" <form action='servlet/ResultServlet?username="
				+ LoginProcess.getInstance().username + ">");
		out.println("<input type='submit' name='click'/>");
		out.println(" </form>");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
